package demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import demo.entity.Student;
import demo.service.StudentService;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/students")
    List<Student> getAllStudents() {
        return studentService.queryAllStudents();
    }

    @GetMapping("/students/{id}")
    Student getStudent(@PathVariable int id) {
        return studentService.queryStudentById(id);
    }

    @PostMapping("/students")
    Student addStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    @PutMapping("/students/{id}")
    Student updateStudent(@RequestBody Student student, @PathVariable int id){
        return studentService.updateStudentById(id,student);
    }

    @DeleteMapping("/students/{id}")
    void deleteStudent(@PathVariable int id) {
        studentService.deleteStudentById(id);
    }
}
